import { TOUCH_MOVE_LEN } from "../base/GameIndex";
import { Tools } from "../common/Tools";
import MaskItem from "../item/Common/MaskItem";
import LevelBase from "../LevelBase";
import LvItem from "../LvItem";
import { LevelManager } from "../manager/LevelManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class magItem extends cc.Component {
    @property({ type: MaskItem, displayName: "控制刷新遮罩组件" })
    maskItem: MaskItem = null;
    @property({ type: cc.Node, displayName: "控制是否可点击的父节点" })
    maskClickNode: cc.Node = null;

    isClick: boolean = true;
    huaPre = null;
    onLoad() {
        let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
        this.huaPre = levelBase.getChildForName("huaPre");


        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartBack, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchMoveEndBack, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchMoveEndBack, this);
    }
    protected onDestroy(): void {
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStartBack, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchMoveEndBack, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchMoveEndBack, this);
    }
    // start () {
    // }
    // update(dt: number): void {
    // }
    touchStartPos: cc.Vec3 = null;
    isTouchMove: boolean = false;
    /** 触摸开始回调 */
    touchStartBack(touches: cc.Event.EventTouch) {
        if (!this.isClick) { return; }
        this.touchStartPos = cc.v3(touches.getLocation());
        this.isTouchMove = false;
        this.node.getComponent(cc.BlockInputEvents).enabled = true;
    };
    /** 触摸移动回调 */
    touchMoveBack(touches: cc.Event.EventTouch) {
        if (!this.isClick) { return; }
        if (!this.touchStartPos || Tools.getDistance(this.touchStartPos, touches.getLocation()) <= TOUCH_MOVE_LEN) { return; }
        this.isTouchMove = true;
        let moveScale = this.node.scale / this.getWorldScale(this.node.scale, this.node);
        if (Math.abs(this.node.position.x + cc.v3(touches.getDelta().multiplyScalar(moveScale)).x) >= this.huaPre.width / 2) {
            return;
        }
        if (Math.abs(this.node.position.y + cc.v3(touches.getDelta().multiplyScalar(moveScale)).y) >= this.huaPre.height / 2) {
            return;
        }
        this.node.position = this.node.position.add(cc.v3(touches.getDelta().multiplyScalar(moveScale)));
        if (this.node)
            this.maskItem.updateRubOut(cc.v2(Tools.getToWorldPosAR(this.node)));
    };
    /** 触摸移动结束回调 */
    touchMoveEndBack(touches: cc.Event.EventTouch) {
        if (!this.isClick) { return; }
        this.runInterNode();

        if (!this.isTouchMove) {
            this.clickItem(touches);
        }
    };
    /** 执行碰撞的检测 */
    runInterNode() {
        let lvNodes = this.maskClickNode.getComponentsInChildren(LvItem);
        let worldPos = Tools.getToWorldPosAR(this.node);
        let tempLen = this.maskItem.maskLen * 0.5;

        for (let i = 0; i < lvNodes.length; i++) {
            let pos = Tools.getToNodePosForNode(lvNodes[i].node, this.node.parent);
            if (Tools.getDistance(pos, this.node.position) <= tempLen
                || lvNodes[i].node.getBoundingBoxToWorld().contains(cc.v2(worldPos))) {
                lvNodes[i].isClick = true;
            } else {
                lvNodes[i].isClick = false;
            }
        }

        let itemNodes = this.maskClickNode.getComponentsInChildren("LvItem_" + LevelManager.ins.curLvInfo.type)
        for (let i = 0; i < itemNodes.length; i++) {
            let pos = Tools.getToNodePosForNode(itemNodes[i].node, this.node.parent);
            if (Tools.getDistance(pos, this.node.position) <= tempLen
                || itemNodes[i].node.getBoundingBoxToWorld().contains(cc.v2(worldPos))
                || itemNodes[i].node.getBoundingBoxToWorld().intersects(this.node.getBoundingBoxToWorld())) {
                itemNodes[i].isClick = true;
            } else {
                itemNodes[i].isClick = false;
            }
        }
    };
    /** 点击item */
    clickItem(touches: cc.Event.EventTouch) {
        let itemNodes = this.maskClickNode.getComponentsInChildren("LvItem_" + LevelManager.ins.curLvInfo.type);
        for (let i = 0; i < itemNodes.length; i++) {
            // let tempPos = Tools.getToNodePosForWorld(touches.getLocation() , itemNodes[i].node );
            // let pos = Tools.getToNodePosForNode(itemNodes[i].node,this.node.parent);
            if (itemNodes[i].isClick && itemNodes[i].node.activeInHierarchy
                && itemNodes[i].node.getBoundingBoxToWorld().contains(touches.getLocation())
                && itemNodes[i].clickItem) {
                itemNodes[i].clickItem();
                // itemNodes[i].node.active = false;
                return;
            }
        }
    };
    /** 获取世界Scale */
    getWorldScale(tempScale: number = 1, node: cc.Node) {
        if (node.parent) {
            return this.getWorldScale(tempScale * node.parent.scale, node.parent);
        }
        return tempScale;
    };

    protected onEnable(): void {
        this.runInterNode();
    }
}
